package com.sise.wangzhan.client;

import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommandProperties;
import com.sise.wangzhan.command.ThreadCommand;

/**
 * @Classname ThreadCommandClient2
 * @Description TODO    信号量隔离策略
 * @Date 2020/4/30 10:19
 * @Created by wangzhan
 */
public class ThreadCommandClient2 {
    public static void main(String[] args) throws InterruptedException {

        //配置使用信号量的策略进行隔离
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.execution.isolation.strategy", HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE
        );
        //设置最大并发数，默认值为10，此处设置为2
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests",2
        );
        //设置执行回退方法的最大并发，默认为10，此处设置为20
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests",20
        );

        for (int i = 1; i <= 20; i++) {
            final int index = i;
            Thread t = new Thread(){
                @Override
                public void run(){
                    ThreadCommand tc = new ThreadCommand(index);
                    tc.execute();
                }
            };
            t.start();
        }
        Thread.sleep(3000);
    }
}
